package com.dianping.cat.home.dependency.graph.transform;

import com.dianping.cat.home.dependency.graph.IEntity;
import com.dianping.cat.home.dependency.graph.IVisitor;
import com.dianping.cat.home.dependency.graph.entity.TopologyEdge;
import com.dianping.cat.home.dependency.graph.entity.TopologyGraph;
import com.dianping.cat.home.dependency.graph.entity.TopologyNode;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;



















public class DefaultJsonBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  38 */   public DefaultJsonBuilder() { this(false); }



/*  42 */   public DefaultJsonBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }


  public DefaultJsonBuilder(boolean compact, StringBuilder sb) {
/*  46 */     this.m_compact = compact;
/*  47 */     this.m_sb = sb;
/*  48 */     this.m_visitor = this;
  }

  protected void arrayBegin(String name) {
/*  52 */     indent();
/*  53 */     this.m_sb.append('"').append(name).append(this.m_compact ? "\":[" : "\": [\r\n");
/*  54 */     this.m_level++;
  }

  protected void arrayEnd(String name) {
/*  58 */     this.m_level--;

/*  60 */     trimComma();
/*  61 */     indent();
/*  62 */     this.m_sb.append("],").append(this.m_compact ? "" : "\r\n");
  }

  protected void attributes(Map<String, String> dynamicAttributes, Object... nameValues) {
/*  66 */     int len = nameValues.length;

/*  68 */     for (int i = 0; i + 1 < len; i += 2) {
/*  69 */       Object attrName = nameValues[i];
/*  70 */       Object attrValue = nameValues[i + 1];

/*  72 */       if (attrValue != null) {
/*  73 */         if (attrValue instanceof Collection) {

/*  75 */           Collection<Object> items = (Collection)attrValue;

/*  77 */           if (!items.isEmpty()) {
            indent();
/*  79 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  81 */             for (Object item : items) {
/*  82 */               this.m_sb.append(' ');
/*  83 */               toString(this.m_sb, item);
/*  84 */               this.m_sb.append(',');
            }

/*  87 */             this.m_sb.setLength(this.m_sb.length() - 1);
/*  88 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
/*  90 */         } else if (attrValue.getClass().isArray()) {
/*  91 */           int length = Array.getLength(attrValue);

/*  93 */           if (length > 0) {
/*  94 */             indent();
            this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  97 */             for (int j = 0; j < length; j++) {
/*  98 */               Object item = Array.get(attrValue, j);
/*  99 */               this.m_sb.append(' ');
/* 100 */               toString(this.m_sb, item);
/* 101 */               this.m_sb.append(',');
            }

/* 104 */             this.m_sb.setLength(this.m_sb.length() - 1);
/* 105 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }

/* 108 */         } else if (this.m_compact) {
/* 109 */           this.m_sb.append('"').append(attrName).append("\":");
/* 110 */           toString(this.m_sb, attrValue);
/* 111 */           this.m_sb.append(",");
        } else {
/* 113 */           indent();
/* 114 */           this.m_sb.append('"').append(attrName).append("\": ");
/* 115 */           toString(this.m_sb, attrValue);
/* 116 */           this.m_sb.append(",\r\n");
        }
      }
    }


/* 122 */     if (dynamicAttributes != null) {
/* 123 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 124 */         if (this.m_compact) {
/* 125 */           this.m_sb.append('"').append((String)e.getKey()).append("\":");
/* 126 */           toString(this.m_sb, e.getValue());
/* 127 */           this.m_sb.append(","); continue;
        }
/* 129 */         indent();
/* 130 */         this.m_sb.append('"').append((String)e.getKey()).append("\": ");
/* 131 */         toString(this.m_sb, e.getValue());
/* 132 */         this.m_sb.append(",\r\n");
      }
    }
  }


  public String build(IEntity<?> entity) {
/* 139 */     objectBegin(null);
/* 140 */     entity.accept(this);
/* 141 */     objectEnd(null);
/* 142 */     trimComma();

/* 144 */     return this.m_sb.toString();
  }

  public String buildArray(Collection<? extends IEntity<?>> entities) {
/* 148 */     this.m_sb.append('[');

/* 150 */     if (entities != null) {
/* 151 */       for (IEntity<?> entity : entities) {
/* 152 */         objectBegin(null);
/* 153 */         entity.accept(this);
/* 154 */         objectEnd(null);
      }

/* 157 */       trimComma();
    }

/* 160 */     this.m_sb.append(']');

/* 162 */     return this.m_sb.toString();
  }

  protected void indent() {
/* 166 */     if (!this.m_compact) {
/* 167 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 168 */         this.m_sb.append("   ");
      }
    }
  }

  protected void objectBegin(String name) {
/* 174 */     indent();

/* 176 */     if (name == null) {
/* 177 */       this.m_sb.append("{").append(this.m_compact ? "" : "\r\n");
    } else {
/* 179 */       this.m_sb.append('"').append(name).append(this.m_compact ? "\":{" : "\": {\r\n");
    }

/* 182 */     this.m_level++;
  }

  protected void objectEnd(String name) {
/* 186 */     this.m_level--;

/* 188 */     trimComma();
/* 189 */     indent();
/* 190 */     this.m_sb.append(this.m_compact ? "}," : "},\r\n");
  }

  protected void toString(StringBuilder sb, Object value) {
/* 194 */     if (value == null) {
/* 195 */       sb.append("null");
/* 196 */     } else if (value instanceof Boolean || value instanceof Number) {
/* 197 */       sb.append(value);
    } else {
/* 199 */       String val = value.toString();
/* 200 */       int len = val.length();

/* 202 */       sb.append('"');

/* 204 */       for (int i = 0; i < len; i++) {
/* 205 */         char ch = val.charAt(i);

/* 207 */         switch (ch) {
          case '"':
          case '/':
          case '\\':
/* 211 */             sb.append('\\').append(ch);
            break;
          case '\t':
/* 214 */             sb.append("\\t");
            break;
          case '\r':
/* 217 */             sb.append("\\r");
            break;
          case '\n':
/* 220 */             sb.append("\\n");
            break;
          default:
/* 223 */             sb.append(ch);
            break;
        }

      }
/* 228 */       sb.append('"');
    }
  }

  protected void trimComma() {
/* 233 */     int len = this.m_sb.length();

/* 235 */     if (this.m_compact) {
/* 236 */       if (len > 1 && this.m_sb.charAt(len - 1) == ',') {
/* 237 */         this.m_sb.replace(len - 1, len, "");
      }
    }
/* 240 */     else if (len > 3 && this.m_sb.charAt(len - 3) == ',') {
/* 241 */       this.m_sb.replace(len - 3, len - 2, "");
    }
  }




/* 248 */   public void visitTopologyEdge(TopologyEdge topologyEdge) { attributes(null, new Object[] { "key", topologyEdge.getKey(), "type", topologyEdge.getType(), "target", topologyEdge.getTarget(), "opposite", Boolean.valueOf(topologyEdge.isOpposite()), "weight", Integer.valueOf(topologyEdge.getWeight()), "status", Integer.valueOf(topologyEdge.getStatus()), "des", topologyEdge.getDes(), "link", topologyEdge.getLink(), "self", topologyEdge.getSelf(), "dashed", Boolean.valueOf(topologyEdge.isDashed()) }); }



  public void visitTopologyGraph(TopologyGraph topologyGraph) {
/* 253 */     attributes(null, new Object[] { "id", topologyGraph.getId(), "type", topologyGraph.getType(), "status", Integer.valueOf(topologyGraph.getStatus()), "des", topologyGraph.getDes() });

/* 255 */     if (!topologyGraph.getNodes().isEmpty()) {
/* 256 */       objectBegin("nodes");

/* 258 */       for (Map.Entry<String, TopologyNode> e : topologyGraph.getNodes().entrySet()) {
/* 259 */         String key = String.valueOf(e.getKey());

/* 261 */         objectBegin(key);
/* 262 */         ((TopologyNode)e.getValue()).accept(this.m_visitor);
/* 263 */         objectEnd(key);
      }

/* 266 */       objectEnd("nodes");
    }

/* 269 */     if (!topologyGraph.getEdges().isEmpty()) {
/* 270 */       objectBegin("edges");

/* 272 */       for (Map.Entry<String, TopologyEdge> e : topologyGraph.getEdges().entrySet()) {
/* 273 */         String key = String.valueOf(e.getKey());

/* 275 */         objectBegin(key);
/* 276 */         ((TopologyEdge)e.getValue()).accept(this.m_visitor);
/* 277 */         objectEnd(key);
      }

/* 280 */       objectEnd("edges");
    }
  }



/* 286 */   public void visitTopologyNode(TopologyNode topologyNode) { attributes(null, new Object[] { "id", topologyNode.getId(), "type", topologyNode.getType(), "status", Integer.valueOf(topologyNode.getStatus()), "des", topologyNode.getDes(), "link", topologyNode.getLink(), "weight", Integer.valueOf(topologyNode.getWeight()) }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/dependency/graph/transform/DefaultJsonBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */